# -*- coding: utf-8 -*-
# Version: Python 3.9.7
# Author: TRIX
# Date: 2021-11-18 21:46:28
# Use: 利用selenium自动评分在网页上提交的作业 可以参照此模板进行修改

from selenium import webdriver#浏览器驱动器
from msedge.selenium_tools import EdgeOptions#Edge浏览器配置
from selenium.webdriver.common.by import By#locator 参数
from selenium.webdriver.support import expected_conditions as EC#条件判断
from selenium.webdriver.support.wait import WebDriverWait#网络等待
from time import sleep

def openDriver(BrowserPath=r'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe',insecure=False,headless=False):
	'''打开浏览器驱动器
	BrowserPath 浏览器路径
	insecure 是否为不安全模式
	headless 是否为无界面模式'''
	driverOptions = EdgeOptions()#Edge浏览器配置
	driverOptions.use_chromium = True#使用chormium内核
	if insecure:#如果为不安全模式
		driverOptions.add_argument('--allow-running-insecure-content')#允许不安全内容
		driverOptions.add_argument("--ignore-certificate-errors")#忽略证书错误
	if headless:#如果为无界面模式
		driverOptions.add_argument('headless')#无界面
		driverOptions.add_argument('disable-gpu')#禁用GPU
	driver = webdriver.Chrome(executable_path=BrowserPath, options=driverOptions)#启动浏览器驱动器
	return driver#返回浏览器驱动器对象 并启动

def scoring(siteInfoDict,homeworkIndex=1):
	'''信息字典 作业序号 从上到下 从1开始'''
	driver.get(siteInfoDict['basicUrl'])#转到链接

	#login
	wait.until(EC.presence_of_all_elements_located((By.TAG_NAME,'input')))#等待元素出现
	inputAreaE=driver.find_elements(By.TAG_NAME,'input')#找到元素
	inputAreaE[0].send_keys(siteInfoDict['account'])#向input元素发送数据
	inputAreaE[1].send_keys(siteInfoDict['password'])
	wait.until(EC.presence_of_all_elements_located((By.ID,'login-button-1')))
	loginbutton=driver.find_element(By.ID,'login-button-1')
	loginbutton.click()#点击元素

	wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME,'class-info-subject')))
	classButtons=driver.find_elements(By.CLASS_NAME,'class-info-subject')
	classButtons[1].click()

	wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,f'#interaction-list-box > div.interaction-group > div.interaction-rows > div:nth-child({homeworkIndex}) > div > div:nth-child(1) > span.interaction-name.grey.fontsize-14.color-33')))
	homeworkButton=driver.find_element(By.CSS_SELECTOR,f'#interaction-list-box > div.interaction-group > div.interaction-rows > div:nth-child({homeworkIndex}) > div > div:nth-child(1) > span.interaction-name.grey.fontsize-14.color-33')
	homeworkButton.click()

	#评分
	unsend=[]
	for n in range(4,89):#展开评分项
		try:
			wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,f'#cc-main > div:nth-child(4) > div:nth-child({n}) > div.appraised-box.cl > div > span.appraise-button.appraised-button-enable')))
			driver.find_element(By.CSS_SELECTOR,f'#cc-main > div:nth-child(4) > div:nth-child({n}) > div.appraised-box.cl > div > span.appraise-button.appraised-button-enable').click()

			wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,f'#cc-main > div:nth-child(4) > div:nth-child({n}) > div.item-score-box > div.score-points-item.cl > ul > li:nth-child(1)')))#等待元素可以被点击
			driver.find_element(By.CSS_SELECTOR,f'#cc-main > div:nth-child(4) > div:nth-child({n}) > div.item-score-box > div.score-points-item.cl > ul > li:nth-child(1)').click()
			print(f'正在初始化第{n-3}个评分')
		except Exception:
			print(f'第{n-3}个同学未提交作业')
			name=driver.find_element(By.XPATH,f'/html/body/div[3]/div[4]/div[{n-3}]/div[1]/div[2]/span').text
			sid=driver.find_element(By.XPATH,f'/html/body/div[3]/div[4]/div[{n-3}]/div[1]/div[2]/div').text#选择元素文字
			unsend.append(name+' '+sid)

	wait.until(EC.presence_of_all_elements_located((By.ID,'score')))
	scoreButtons=driver.find_elements(By.ID,'score')
	for i,button in enumerate(scoreButtons):#进行评分
		button.click()
		sleep(0.3)
		print(f'第{i+1}个同学评分成功')
	print(f'有{len(unsend)}个同学未提交作业 评分失败 分别为')
	for n in unsend:
		print(n)

	driver.close()
	driver.quit()

driver=openDriver(headless=True)#启动浏览器驱动器
wait=WebDriverWait(driver, 5)#设置等待 超时时间
siteInfoDict={#信息
'basicUrl':'',#将要selenium操作的url
'account':'',
'password':'',
}
def writeMyAccount():#这里使用的是个人账号
	with open('myAccount.txt','r',encoding='utf-8') as f:
		accountInfo=f.read().split()
	siteInfoDict['basicUrl']=accountInfo[0]
	siteInfoDict['account']=accountInfo[1]
	siteInfoDict['password']=accountInfo[2]
writeMyAccount()
scoring(siteInfoDict)
